package com.hzit.search.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Configuration
@Data
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig {
	private String host;
	private int port;

	@Bean
	public ElasticsearchClient elasticsearchClient(){
		//1. 定义RestClient对象
		RestClient restClient = RestClient.builder(new HttpHost(host,port)).build();
		//2. 定义ElasticsearchTransport对象
		ElasticsearchTransport transport = new RestClientTransport(restClient,new JacksonJsonpMapper());
		//3. 定义elasticsearchClient对象
		ElasticsearchClient elasticsearchClient = new ElasticsearchClient(transport);
		//4. 返回
		return elasticsearchClient;
	}
}
